/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.xwiki.test.integration;

import org.apache.commons.exec.LogOutputStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.encoder.PatternLayoutEncoder;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.ConsoleAppender;

/**
 * Logs the stdout and stderr streams of an external process started using Commons Exec.
 *
 * @version $Id$
 * @since 4.3.1
 */
public class XWikiLogOutputStream extends LogOutputStream
{
    /**
     * Represents the stdout stream.
     */
    private static final int STDOUT = 0;

    /**
     * The logger to use logs generated by the XWiki process.
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(XWikiLogOutputStream.class);

    /**
     * Always log at STDOUT level since we don't care as we'll log exactly the messages passed to us.
     */
    public XWikiLogOutputStream()
    {
        super(STDOUT);
        configureLogger();
    }

    @Override
    protected void processLine(String line, int level)
    {
        // Note: We log as "info" level since we don't care as we don't display the logging level. We do that to
        // avoid duplicate logging levels (XWiki logs already contain logging levels). There's also a current limitation
        // since our Jetty config logs everything as stderr.
        LOGGER.info(line);
    }

    /**
     * Programmatically configure LogBack so that we log the message being passed without printing the logging level.
     */
    private void configureLogger()
    {
        LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
        PatternLayoutEncoder ple = new PatternLayoutEncoder();
        ple.setPattern("%d [%t] - %m %n");
        ple.setContext(lc);
        ple.start();

        ConsoleAppender<ILoggingEvent> consoleAppender = new ConsoleAppender<>();
        consoleAppender.setTarget("System.out");
        consoleAppender.setEncoder(ple);
        consoleAppender.setContext(lc);
        consoleAppender.start();

        ch.qos.logback.classic.Logger logger =
            (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(XWikiLogOutputStream.class);
        logger.detachAndStopAllAppenders();
        logger.addAppender(consoleAppender);
        logger.setLevel(Level.INFO);
        logger.setAdditive(false);
    }
}
